<?php

namespace App\Api\Requests;

use Illuminate\Validation\Rule;

class AdminRoleRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('adminRole');//获取当前需要排除的id,这里的 adminRole 是 路由put {} 中的参数
        switch ($this->method()) {
            // CREATE
            case 'POST':
            {
                return [
                    'name' => 'required|min:3|max:20',
                    'slug' => "required|min:3|unique:admin_roles",
                ];
            }
            // UPDATE
            case 'PUT':
                $row = $this->route('id');
                return [
                    'name' => 'required|min:3|max:20',
                    'slug' => "required|min:3|unique:admin_roles,slug,{$id}",//table,column,except,idColumn
                ];
        }
    }

    /** 定义规则验证失败后的消息
     * @return array
     */
    public function messages()
    {
        return parent::messages(); // TODO: Change the autogenerated stub
    }

    /** 定义字段说明
     * @return array
     */
    public function attributes()
    {
        return [
            'name' => '角色名称',
            'slug' => '角色标识符'
        ];
    }
}
